//
//  FilterImage.m
//  FengWoFilterTickerDemo
//
//  Created by yang on 15/10/29.
//  Copyright © 2015年 FengWo. All rights reserved.
//

#import "FilterImage.h"
#import "GPUImage.h"

@interface  FilterImage()
{
    GPUImagePicture* sourcePicture;
    GPUImageSepiaFilter* sepiaFilter;
    NSMutableArray *imageArray;
}
@property (strong,nonatomic) UIImage *inputImage;
@end

@implementation FilterImage


- (NSArray *)getFilterImagesFromInputImage:(UIImage *)inImage
{
    UIImage *inputImage = [self thumbnailWithImage:inImage];
    imageArray = [NSMutableArray array];
    [imageArray addObject:inputImage];
    for (int i=1; i<15; i++) {
        [imageArray addObject:[self selectFilterImageWithTag:i inputImage:inputImage]];
    }
    return imageArray;
}


- (UIImage *)selectFilterImageWithTag:(NSInteger)tag inputImage:(UIImage *)inputImage
{
    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage];
    self.inputImage = inputImage;
    UIImage *outputImage;
    
    switch (tag) {
        case 1:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"aimei" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];
        }
            break;
            
        case 2:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"danlan" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        case 3:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"danhuang" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

           }
            break;
        case 4:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"fugu" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        case 5:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"gaoleng" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];
//            
        }
            break;
        case 6:
        {
            
            NSString *path = [[NSBundle mainBundle] pathForResource:@"huaijiu" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];
        }
            break;
        case 7:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"jiaopian" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];
        }
            break;
        case 8:
        {

            NSString *path = [[NSBundle mainBundle] pathForResource:@"keai" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        case 9:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"lomo" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];
        }
            break;
        case 10:
        {

            NSString *path = [[NSBundle mainBundle] pathForResource:@"morenjiaqiang" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];


        }
            break;
        case 11:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"nuanxin" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        case 12:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"qingxin" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];


        }
            break;
        case 13:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"rixi" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        case 14:
        {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"wennuan" ofType:@".acv"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            GPUImageToneCurveFilter *f1 = [[GPUImageToneCurveFilter alloc] initWithACVData:data];
            [sourcePicture addTarget:f1];
            [f1 useNextFrameForImageCapture];
            
            [sourcePicture processImage];
            
            outputImage = [f1 imageFromCurrentFramebuffer];

        }
            break;
        default:
            break;
    }

    return outputImage;
}

- (UIImage *)setupSelectFiltering:(GPUImageFilter*)filter
{
    sourcePicture = [[GPUImagePicture alloc] initWithImage:self.inputImage smoothlyScaleOutput:YES];
    sepiaFilter = (GPUImageSepiaFilter *)filter;
    [sepiaFilter useNextFrameForImageCapture];
    
    GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:self.inputImage];
    [imageSource addTarget:sepiaFilter];
    [imageSource processImage];
    UIImage *newImage2 = [sepiaFilter imageFromCurrentFramebuffer];
    return newImage2;
}

//压缩图片
- (UIImage *)thumbnailWithImage:(UIImage *)image
{
    UIImage *newimage;
    CGSize asize = CGSizeMake(image.size.width*0.5, image.size.height*0.5);
    if (nil == image) {
        newimage = nil;
    }
    else{
        UIGraphicsBeginImageContext(asize);
        [image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return newimage;
}


@end
